﻿using System;
using UwaySoft.EsheetServer.BizLogic;
using UwaySoft.EsheetServer.DataLayer;
using UwaySoft.EsheetServer.Help.Template;
using System.Xml;
using UwaySoft.EsheetServer.Help.Response;

namespace UwaySoft.EsheetServer.Help.WorkDAL
{
    public class CheckHangDAL : IWorkDAL
    {

        protected ISqlHelper SqlHelper
        {
            get { return Application.SqlHelper; }
        }

        #region IDemoWorkOrder 成员

        public IResponse DoWork(string xmlString)
        {
            XmlDocument xmlDoc = null;
            try
            {
                xmlDoc = XmlHelper.ReadAsXmlDocument(xmlString);
            }
            catch (Exception exp)
            {
                Log.Debug(string.Format("审批挂起申请信息 发送文字不符合XML格式，无法转换解析！\r\n{0}", xmlString));
                Log.Error(exp);
                return new NewSheetResponse(false, "-1", "-1", @"审批挂起申请信息 发送文字不符合XML格式，无法转换解析！");
            }

            CheckHangEntity entity = null;
            try
            {
                entity = new CheckHangEntity(xmlDoc);
            }
            catch (Exception exp)
            {
                Log.Debug(string.Format("审批挂起申请信息 无法解析文档\r\n{0}", xmlString));
                Log.Error(exp);
                return new NewSheetResponse(false, "-1", "-1", @"审批挂起申请信息 无法解析文档");
            }
            return AddDemoWorkOrder(entity);
        }

        #endregion

        protected CheckHangResponse AddDemoWorkOrder(CheckHangEntity workOrderDemo)
        {
            var workOrderId = Guid.NewGuid();
            try
            {
                SqlHelper.ExecuteNonQuery(@"INSERT INTO MOD_WORKORDERDEMO
                                        VALUES
                                          (:SERIALNO,
                                           :STATE,
                                           :SERVICECLASSID,
                                           :SERVICECONTENT,
                                           :ACCEPTNO,
                                           :ACCEPTMGR,
                                           :ACCEPTPHONE,
                                           :USERNAME,
                                           :USERGRADE,
                                           :MAINCALLNO,
                                           :CONTACTNAME,
                                           :CONTACTID1,
                                           :CONTACTID2,
                                           :FAX,
                                           :EMAIL,
                                           :ADDRESS,
                                           :FAULTHAPPENDATE,
                                           :LASTDATE,
                                           :WORKORDERID,
                                           DEFAULT)",
                                          SqlHelper.CreateParameter(":SERIALNO", workOrderDemo.SerialNo),
                                          SqlHelper.CreateParameter(":STATE", workOrderDemo.State),
                                          SqlHelper.CreateParameter(":SERVICECLASSID", workOrderDemo.ServiceClassId),
                                          SqlHelper.CreateParameter(":SERVICECONTENT", workOrderDemo.ServiceContent),
                                          SqlHelper.CreateParameter(":ACCEPTNO", workOrderDemo.AcceptNo),
                                          SqlHelper.CreateParameter(":ACCEPTMGR", workOrderDemo.AcceptMgr),
                                          SqlHelper.CreateParameter(":ACCEPTPHONE", workOrderDemo.AcceptPhone),
                                          SqlHelper.CreateParameter(":USERNAME", workOrderDemo.UserName),
                                          SqlHelper.CreateParameter(":USERGRADE", workOrderDemo.UserGrade),
                                          SqlHelper.CreateParameter(":MAINCALLNO", workOrderDemo.MainCallNo),
                                          SqlHelper.CreateParameter(":CONTACTNAME", workOrderDemo.ContactName),
                                          SqlHelper.CreateParameter(":CONTACTID1", workOrderDemo.ContactId1),
                                          SqlHelper.CreateParameter(":CONTACTID2", workOrderDemo.ContactId2),
                                          SqlHelper.CreateParameter(":FAX", workOrderDemo.Fax),
                                          SqlHelper.CreateParameter(":EMAIL", workOrderDemo.Email),
                                          SqlHelper.CreateParameter(":ADDRESS", workOrderDemo.Address),
                                          SqlHelper.CreateParameter(":FAULTHAPPENDATE", workOrderDemo.FaultHappenDate),
                                          SqlHelper.CreateParameter(":LASTDATE", workOrderDemo.LastDate),
                                          SqlHelper.CreateParameter(":WorkOrderId", workOrderId.ToString("N")));

            }
            catch
            {
                return new CheckHangResponse(false, workOrderDemo.SerialNo, "-1", @"审批挂起申请信息 插入数据库失败");
            }
        
            return new CheckHangResponse(true, workOrderDemo.SerialNo, workOrderId.ToString(), @"审批挂起申请信息 接收成功");
        }
    }
}
